{extend name="public/base"}

{block name="title"}
收银台
{/block}

{block name="style"}
{css href="/static/wechat/css/order.css" /}
<style>
    .weui-dialog.weui-dialog--visible {
        display: block;
    }
</style>
{/block}

{block name="body"}
<div class="pageControl pay">
    <div class="msui_nav msui_navTop">
        <a class="msui_navTop-back" href="javascript:history.back();"><i class="micon-back"></i></a>
        <h1 class="msui_navTop-title">收银台</h1>
    </div>
    <div class="weui-top">
        <div class="msui_cell msui_cell-column mt10">
            <div class="msui_cell-item">
                <p class="fs11 cl-orange">¥{$data.order_price}</p>
            </div>
            <div class="msui_cell-item">
                {switch name="model"}
                {case value="order_recharge"}
                <p class="fs08 cl-9999">充值单:{$data.order_sn}</p>
                {/case}
                {case value="scan_pay"}
                <p class="fs08 cl-9999">扫码支付单:{$data.order_sn}</p>
                {/case}
                {default /}
                <p class="fs08 cl-9999">{$data.company_name}服务单:{$data.order_sn}</p>
                {/switch}
            </div>
        </div>
        {if condition="($data.type == 'order') OR ($data.type == 'scan_pay') OR ($data.type == 'deal') "}
        <div class="weui-cells mt10">
            <a  href="javascript:;" class="weui-cell weui-cell_access open-popup coupons" data-target="#coupons">
                <div class="weui-cell__bd">
                    <p class="fs08 cl-3333">优惠券</p>
                </div>
                <div class="weui-cell__ft fs075 cl-orange">¥<span class="coupon_price">0.00</span></div>
            </a>
        </div>
        {/if}
        <div class="weui-cells weui-cells_checkbox js-choose">
            <div class="msui_cells-title fs08 cl-6666">选择支付方式</div>
            <label class="weui-cell weui-check__label">
                <div class="weui-cell__hd">
                    <input type="checkbox" class="weui-check" data-pay-type="weixin">
                    <i class="micon-check"></i>
                </div>
                <div class="weui-cell__bd">
                    <div class="msui_table">
                        <div class="msui_tabCell-img">
                            <img src="/static/wechat/images/payPhoto3.png">
                        </div>
                        <div class="msui_tabCell-content">
                            <p class="fs08 cl-3333">微信</p>
                        </div>
                    </div>
                </div>
            </label>
            {if condition="($data.type == 'order') OR ($data.type == 'deal') OR ($data.type == 'scan_pay')"}
            <label class="weui-cell weui-check__label">
                <div class="weui-cell__hd">
                    <input type="checkbox" class="weui-check" data-pay-type="yue">
                    <i class="micon-check"></i>
                </div>
                <div class="weui-cell__bd">
                    <div class="msui_table">
                        <div class="msui_tabCell-img">
                            <img src="/static/wechat/images/payPhoto1.png">
                        </div>
                        <div class="msui_tabCell-content">
                            <p class="fs08 cl-3333">余额</p>
                        </div>
                    </div>
                </div>
            </label>
            {/if}
        </div>
        <div class="msui_button">
            <div class="msui_button-hd">
                <p class="cl-3333 fs08">待支付:<span class="cl-orange pay_price">￥{$data.order_price}</span></p>
            </div>
            <div class="msui_button-bd">
                <input type="hidden" name="select_coupon" value="0">
                <a  id="payMoney" class="msui_button-content">确认支付</a>
            </div>
        </div>
        <!--优惠券弹出层开始-->
        <div id="coupons" class="weui-popup__container js-coupons">
            <div class="weui-popup__overlay"></div>
            <div class="weui-popup__modal">
                <div class="msui_nav msui_navTop">
                    <a class="msui_navTop-back close-popup"><i class="micon-back"></i></a>
                    <h1 class="msui_navTop-title">选择优惠券</h1>
                </div>
            </div>
        </div>
        <!--优惠券弹出层结束-->
    </div>

</div>
<!-- loading toast -->
<div id="loadingToast" style="display:none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast" style="opacity: 1; visibility: visible;">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content">获取支付数据</p>
    </div>
</div>
{/block}

{block name="javascript"}

<script>
    $(function () {
        var _is_submit_gift = 0;
        $("#payMoney").click(function () {
            var _this = $(this);
            _this.text('正在支付').css('background', '#ddd');
            var member_coupon_id = $("input[name='select_coupon']").length > 0 ? $("input[name='select_coupon']").val() : 0;
            var pay_type = $(".micon-checked").prev().data('pay-type');

            if (pay_type == 'weixin') {
                $('#loadingToast').fadeIn(100);
                $.ajax({
                    type : 'POST',
                    url : '{:url("pay/wx_pay")}',
                    data : {
                        order_id : '{$data.id|default=0}',
                        member_coupon_id : member_coupon_id,
                        model : '{$model}',
                        order_price : '{$data.order_price}',
                        type : '{$data.type|default=""}',
                        member_type : '{$data.member_type|default=0}'
                    },
                    //dataType : 'json',
                    success : function(msg) {
                        if(msg.code == 400) {
                            $.alert(msg.msg);
                            _this.text('确认支付').css('background', '#3cb1ff');
                            $('#loadingToast').fadeOut(100);
                            return false;
                        } else {
                            $("html").append(msg);
                            setTimeout(function() {
                                callpay();
                                $('#loadingToast').fadeIn(100);
                            },200);
                        }
                    }
                });
            } else if (pay_type == 'yue'){
                $('#loadingToast').fadeIn(100);
                var url = '{:url("balance_paid")}';
                var data = {
                    order_id : '{$data.id|default=0}',
                    member_coupon_id : member_coupon_id,
                    model : '{$model}',
                    order_sn : '{$data.order_sn}',
                    order_price : '{$data.order_price}',
                    type : '{$data.type|default=""}',
                    member_type : '{$data.member_type|default=0}'
                };
                safe_password(url, data);
                _this.text('确认支付').css('background', '#3cb1ff');
                return false;
            } else {
                alert('请选择支付方式！');
                _this.text('确认支付').css('background', '#3cb1ff');
                $('#loadingToast').fadeOut(100);
            }
        });

        $(document).on('click', '.js-couponsList', function () {
            var member_coupon_id = $(this).data('coupon-id'); // 获取优惠券ID
            var coupon_price = $(this).data('coupon-price');// 获取优惠金额
            $(".coupon_price").text(coupon_price); // 设置显示优惠券金额
            var order_price = '{$data.order_price}';// 订单金额
            $(".pay_price").text((Number(order_price)*100-Number(coupon_price) *100)/100);// 订单金额 - 优惠金额  =  支付金额
            $("input[name='select_coupon']").val(member_coupon_id);// 保存优惠券ID， 后台需要再次计算， 前台数据并不安全
            $(".weui-popup__overlay").click(); // 模拟点击关闭按钮， 关闭优惠券浮动框
        });

        $(".coupons").click(function () {
            var order_id = '{$data.id|default=0}';
            var model = '{$model}';
            $.get('{:url("WCoupon/index")}', {order_id : order_id, model : model}, function (msg) {
                if (msg.code == 200) {
                    var data = msg.data;
                    var len = data.length;
                    if (len > 0) {
                        var html = [];
                        for(var i = 0; i < len; i++) {
                            html.push('<div class="weui-flex couponsBg msui_table js-couponsList" data-coupon-id="'+ data[i].id +'" data-coupon-price="'+ data[i].money +'">');
                            html.push('    <div class="couponsBg-hd weui-flex ">');
                            html.push('        <span>¥'+ data[i].money +'</span>');
                            html.push('    </div>');
                            html.push('   <div class="weui-flex__item couponsBg-bd">');
                            html.push('        <p class="fs08">'+ data[i].content +'</p>');
                            html.push('        <p class="fs07">有效期 : '+ formattingTimestamp(data[i].start_use_time, 1) +' - '+ formattingTimestamp(data[i].end_use_time, 1) +'</p>');
                            html.push('   </div>');
                            html.push('   <div class="couponsBg-ft weui-flex ">');
                            html.push('        <input type="radio" name="coupons">');
                            html.push('           <i class="micon-check"></i>');
                            html.push('    </div>');
                            html.push('</div>');
                        }
                        $(".weui-popup__modal").append(html.join(''));
                    } else {
                        $(".weui-popup__modal").append('<p>暂无优惠券</p>');
                    }
                } else {
                    $(".weui-popup__modal").append('<p>系统繁忙</p>');
                }
            }, 'json');
        });
    });
</script>
{/block}